﻿<?php
    require_once ("/mnt/sdcard/www/Includes/simplecms-config.php");
    require_once  ("/mnt/sdcard/www/Includes/connectDB.php");
    include("/mnt/sdcard/www/Includes/header.php");

    createLog("phoneEnroll");

    if(isset($_POST['save']))
    {
        $deviceOS = $_POST['deviceOS'];
        $deviceName = $_POST['deviceName'];
        $deviceModel = $_POST['deviceModel'];
        $deviceVersion = $_POST['deviceVersion'];
        $deviceResolution = $_POST['deviceResolution'];
        $team = $_POST['team'];
        $name = $_POST['name'];
        $returnDate = $_POST['returnDate'];
        $carrier = $_POST['carrier'];
        $deviceNumber = $_POST['deviceNumber'];
        $etc = $_POST['etc'];
        $deviceID = $_POST['deviceID'];
        $deviceState = $_POST['deviceState'];
        $deviceOwner = $_POST['deviceOwner'];
        $deleted = 0;

        $query = "INSERT INTO deviceInfo (deviceOS, deviceName, deviceModel, deviceVersion, deviceResolution, team, name, returnDate, carrier, deviceNumber, etc, deviceID, deviceState, deviceOwner, deleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        $statement = $databaseConnection->prepare($query);
        $statement->bind_param('ssssssssssssiii', $deviceOS, $deviceName, $deviceModel, $deviceVersion, $deviceResolution, $team, $name, $returnDate, $carrier, $deviceNumber, $etc, $deviceID, $deviceState, $deviceOwner, $deleted);
        $statement->execute();
        $statement->store_result();

        $creationWasSuccessful = $statement->affected_rows == 1 ? true : false;
        if ($creationWasSuccessful)
        {
            $statement->close();
            echo("
                <script>
                window.alert('저장되었습니다.')
                location.replace('phoneList.php')
                </script>
            ");
            //header ("Location: phoneList.php");
        }
        else
        {
            $statement->close();
            echo("
                <script>
                window.alert('기기등록에 실패했습니다.')
                </script>
            ");
        }
    }
?>

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            </button>
            <?php
                if (logged_on())
                {
                    $url = "/phoneList.php";
                }
                else
                {
                    $url = "/index.php";
                }
            ?>
            <a class="navbar-brand" href="<?php echo $url ?>">Device Management System</a>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li><a href="/index.php">Home</a></li>
                <?php
                    if (logged_on())
                    {
                        echo '
                            <li><a href="/phoneList.php">기기목록</a></li>
                            <li><a href="/approval.php">결재</a></li>
                        ';
                    }
                ?>
                <?php
                    if (is_admin())
                    {
                        echo '
                            <li class="dropdown" class="active">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">관리메뉴<b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    <li><a href="/phoneEnroll.php">기기등록</a></li>
                                    <li><a href="/manage.php">기기관리</a></li>
                                    <!--<li class="divider"></li>
                                    <li class="dropdown-header">Nav header</li>
                                    <li><a href="#">Separated link</a></li>
                                    <li><a href="#">One more separated link</a></li>-->
                                </ul>
                            </li>
                        ';
                    }
                ?>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <?php
                    if (logged_on())
                    {
                        if (is_admin())
                        {
                            echo "
                                <li><a href='#'>환영합니다, {$_SESSION['username']}(관리자)</a></li>
                                <li><a href='/logoff.php'>로그아웃</a></li>
                            ";
                        }
                        elseif (is_member())
                        {
                            echo "
                                <li><a href='#'>환영합니다, {$_SESSION['username']}(정회원)</a></li>
                                <li><a href='/logoff.php'>로그아웃</a></li>
                            ";   
                        }
                        else
                        {
                            echo "
                                <li><a href='#'>환영합니다, {$_SESSION['username']}(준회원)</a></li>
                                <li><a href='/logoff.php'>로그아웃</a></li>
                            ";
                        }
                    }
                ?>
            </ul>
        </div><!--/.navbar-collapse -->
    </div>
</div>

<div class="container">
    <?php
        if(!is_admin())
        {
            echo("
                <script>
                window.alert('페이지에 접근할 수 있는 권한이 없습니다.')
                location.replace('index.php')
                </script>
                ");
            exit;
        }

        $currentDate = date("Y-m-d");
    ?>

    <form class="form" role="form" action="phoneEnroll.php" method="post">
        <div class="form-group col-lg-3">
            <label>기기ID</label>
            <input type="text" name="deviceID" class="form-control" id="inputDeviceID" placeholder="deviceID">
        </div>
        <div class="form-group col-lg-3">
            <label>운영체제</label>
                <select class="form-control" name="deviceOS">
                    <option value="Android">Android</option>
                    <option value="iOS">iOS</option>
                    <option value="etc">기타</option>
                </select>
        </div>
        <div class="form-group col-lg-3">
            <label>기기명</label>
            <input type="text" name="deviceName" class="form-control" id="inputDeviceName" placeholder="deviceName">
        </div>
        <div class="form-group col-lg-3">
            <label>모델명</label>
            <input type="text" name="deviceModel" class="form-control" id="inputDeviceModel" placeholder="deviceModel">
        </div>
        <div class="form-group col-lg-3">
            <label>버전</label>
            <input type="text" name="deviceVersion" class="form-control" id="inputDeviceVersion" placeholder="deviceVersion">
        </div>
        <div class="form-group col-lg-3">
            <label>해상도</label>
            <input type="text" name="deviceResolution" class="form-control" id="inputDeviceResolution" placeholder="1080x1920">
        </div>
        <div class="form-group col-lg-3">
            <label>팀</label>
            <input type="text" name="team" class="form-control" id="inputTeam" placeholder="team" value="<? echo $_SESSION['team'] ?>">
        </div>
        <div class="form-group col-lg-3">
            <label>이름</label>
            <input type="text" name="name" class="form-control" id="inputTeam" placeholder="name" value="<? echo $_SESSION['name'] ?>">
        </div>
        <div class="form-group col-lg-3">
            <label>기기반납일</label>
            <input type="text" name="returnDate" class="form-control" placeholder="<? echo $currentDate ?>" id="dp1">
        </div>
        <div class="form-group col-lg-3">
            <label>통신사</label>
            <select class="form-control" name="carrier">
                <option value="">없음</option>
                <option value="SKT">SKT</option>
                <option value="KT">KT</option>
                <option value="LGT">LGT</option>
            </select>
        </div>
        <div class="form-group col-lg-3">
            <label>번호</label>
            <input type="text" name="deviceNumber" class="form-control" id="inputDeviceNumber" placeholder="010-1234-5836">
        </div>
        <div class="form-group col-lg-3">
            <label>비고</label>
            <textarea class="form-control" name="etc" rows="1"></textarea>
        </div>
        <div class="form-group col-lg-3">
            <label>기기상태</label>
            <select class="form-control" name="deviceState">
                <option value="0">대여가능</option>
                <option value="1">대여중</option>
                <option value="2">개발팀대여중</option>
                <option value="9">수리중</option>
            </select>
        </div>
        <div class="form-group col-lg-3">
            <label>기기소유</label>
            <select class="form-control" name="deviceOwner">
                <option value="0">QA팀</option>
                <option value="1">대여기기</option>
                <option value="2">장기대여기기</option>
                <option value="3">개발팀</option>
            </select>
        </div>
        <div class="form-group col-lg-3">
        <p><button type="submit" name="save" class="btn btn-primary">저장</button></p>
        </div>
    </form>
</div>

<?php 
    include ("/mnt/sdcard/www/Includes/footer.php");
 ?>